package com.zpark.servlet_context;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;

/**
 * @PackageName: com.zpark.servlet_context
 * @ClassName: ServletContextDemo05
 * @Description:
 * @author: RZS
 * @date: 2023/3/8  9:34
 */
@WebServlet("/sc05.do")
public class ServletContextDemo05 extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        /**
         * 获取前面servlet绑定在context域对象中的数据
         * 1、获取context域对象
         * 2、调用获取方法得到数据
         */
        // 获取context对象
        ServletContext servletContext = getServletContext();
        // 根据绑定数据的名字获取内容
        System.out.println(servletContext.getAttribute("username"));
        User user = (User) servletContext.getAttribute("user");
        System.out.println(user);

        int[] array = (int[]) servletContext.getAttribute("array");
        System.out.println(Arrays.toString(array));

        // 移除绑定数据
        servletContext.removeAttribute("request");

        // 获取绑定的request
        System.out.println(servletContext.getAttribute("request"));

        // 获取资源文件的真实路径
        String path = servletContext.getRealPath("/index.jsp");
        System.out.println(path);
    }
}
